/*
 * topological-hex, a program to compute combinatorial hexahedral meshes.
 *
 * Copyright (C) <2018> <Université catholique de Louvain (UCL), Belgique>
 *
 * List of the contributors to the development, description and complete
 * License: see LICENSE file.
 *
 * This program (topological-hex) is free software:
 * you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program (see COPYING file).  If not,
 * see <http://www.gnu.org/licenses/>.
 */

#include "errors.h"

#include <stdio.h>
#include <stdarg.h>

static void default_log_fn(const char *string, void *user_data);

static error_log_fn callback = default_log_fn;
static void *callback_data = NULL;

void set_error_log_fn(error_log_fn fn, void *user_data) {
  callback = fn ? fn : default_log_fn;
  callback_data = user_data;
}

void log_error(const char *fmt, ...) {
  char buffer[1024];

  va_list list;
  va_start(list, fmt);
  vsnprintf(buffer, sizeof(buffer), fmt, list);
  va_end(list);

  callback(buffer, callback_data);
}

static void default_log_fn(const char *message, void *user_data) {
  fprintf(stderr, "%s\n", message);
}
